package com.black.app.service.impl.store;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.app.domain.req.my.myorders.MyOrderAddReq;
import com.black.app.domain.req.my.myorders.MyOrdersEditReq;
import com.black.app.domain.req.my.myorders.MyOrdersQueryReq;
import com.black.app.domain.resp.my.myorders.MyOrdersQueryResp;
import com.black.app.domain.resp.my.myorders.UserOrderDetails;
import com.black.app.domain.resp.my.myorders.UserOrdersResp;
import com.black.app.domain.store.AppStoreOrders;
import com.black.app.mapper.store.AppStoreOrdersMapper;
import com.black.app.service.store.AppStoreOrdersService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author admin
* @description 针对表【app_store_orders】的数据库操作Service实现
* @createDate 2024-10-28 11:46:37
*/
@Service
public class AppStoreOrdersServiceImpl extends ServiceImpl<AppStoreOrdersMapper, AppStoreOrders>
    implements AppStoreOrdersService{

    /**
     * 分页查询订单列表
     * @param req
     * @return
     */
    @Override
    public PageInfo<MyOrdersQueryResp> paginationQuery(MyOrdersQueryReq req) {
        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(()->{
            this.baseMapper.paginationQuery(req);
        });
    }

    /**
     * 获取用户订单列表
     * @param userId
     * @return
     */
    @Override
    public PageInfo<UserOrdersResp> userOrderList(Long userId) {
        return PageHelper.startPage(1,5).doSelectPageInfo(()->{
            this.baseMapper.userOrderList(userId);
        });
    }

    /**
     * 根据订单编号获取订单详情
     * @param orderCode
     * @return
     */
    @Override
    public UserOrderDetails findByCode(String orderCode) {
        return this.baseMapper.getOrderDetails(orderCode);
    }

    /**
     * 新增订单
     * @param req
     * @return
     */
    @Override
    public boolean add(MyOrderAddReq req) {
        AppStoreOrders orders = new AppStoreOrders();
        //对象拷贝
        BeanUtils.copyProperties(req, orders);
        return save(orders);
    }

    /**
     * 修改订单(只能修改收获人信息)
     * @param req
     * @return
     */
    @Override
    public boolean edit(MyOrdersEditReq req) {
        Integer orderId = this.baseMapper.getOrderId(req.getOrderCode());
        AppStoreOrders orders = getById(orderId);
        orders.setConsigneeAddress(req.getConsigneeAddress());
        orders.setConsigneeName(req.getConsigneeName());
        orders.setConsigneePhone(req.getConsigneePhone());
        return this.updateById(orders);
    }

    /**
     * 根据订单编号删除订单
     * @param orderCodes
     * @return
     */
    @Override
    public boolean deleteByCode(String[] orderCodes) {
        Integer[] orderIds = new Integer[orderCodes.length];
        for (int i = 0; i < orderCodes.length; i++){
            orderIds[i] = this.baseMapper.getOrderId(orderCodes[i]);
        }
        if(orderIds.length == 0){
            throw new RuntimeException("订单编号不存在");
        }
        for (int j = 0; j < orderIds.length; j++){
            AppStoreOrders order = this.getById(orderIds[j]);
            if(Objects.nonNull(order)){
                this.removeById(orderIds[j]);
            }
        }
        return true;
    }


}




